﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using famails.common;
using famails.dto;
using famails.App_Code.source.dto;

namespace famails.dao
{
    /// <summary>
    /// Summary description for DiscountDTO
    /// </summary>
    public class CM_DiscountDAO
    {
        public CM_DiscountDAO()
        {

        }
        public DataTable GetByID(decimal ID, int UserID)
        {
            SqlCommand cmd = new SqlCommand("[SP_CM_Discount_Get4Edit] "+ID+","+UserID, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetAll(int userid)
        {
            SqlCommand cmd = new SqlCommand("SELECT a.*, subName UserName, subEmail FROM tblCM_Discounts a left join tblsubclient s on s.subId = a.userid WHERE a.isdelete <> 1 and createby="+userid, ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable Search(string Code, string Name,int userid)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Discount_Search ", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = Name;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = Code;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = userid;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
        public DataTable Search(string Name, string Code, int userid, string from, string to)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Discount_Search", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = Name;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = Code;
            cmd.Parameters.Add("@from", SqlDbType.NVarChar).Value = from;
            cmd.Parameters.Add("@to", SqlDbType.NVarChar).Value = to;
            cmd.Parameters.Add("@userid", SqlDbType.Int).Value = userid;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public object Insert(CM_DiscountDTO dto)
        {
            SqlCommand cmd = new SqlCommand("insert into tblCM_Discounts(DiscountName, DiscountCode, Fix0_Per1, Notes, NumberOfCode, MinAmount, DiscountValue, FromDate, ToDate, CreateDate, createBy)values(@name, @code, @Fix0_Per1, @Notes, @NumberOfCode, @MinAmount, @DiscountValue, @FromDate, @ToDate, getdate(), @createby) select @@identity", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@MinAmount", SqlDbType.Decimal).Value = dto.MinAmount;
            cmd.Parameters.Add("@DiscountValue", SqlDbType.Decimal).Value = dto.DiscountValue;
            cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = dto.Notes;
            cmd.Parameters.Add("@NumberOfCode", SqlDbType.Int).Value = dto.NumberOfCode;
            cmd.Parameters.Add("@Fix0_Per1", SqlDbType.Int).Value = dto.Fix0_Per1;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.DiscountName;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = dto.DiscountCode;
            cmd.Parameters.Add("@FromDate", SqlDbType.Date).Value = dto.FromDate;
            cmd.Parameters.Add("@ToDate", SqlDbType.Date).Value = dto.ToDate;
            cmd.Parameters.Add("@CreateBy", SqlDbType.Int).Value = dto.CreateBy;
            object i = cmd.ExecuteScalar();
            cmd.Dispose();
            return i;
        }

        public bool Edit(CM_DiscountDTO dto)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Discounts set DiscountName=@name, DiscountCode=@code, Fix0_Per1=@Fix0_Per1, Notes=@Notes, NumberOfCode=@NumberOfCode, MinAmount=@MinAmount, FromDate=@FromDate, ToDate=@ToDate, DiscountValue=@DiscountValue where Discountid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = dto.DiscountID;
            cmd.Parameters.Add("@MinAmount", SqlDbType.Decimal).Value = dto.MinAmount;
            cmd.Parameters.Add("@DiscountValue", SqlDbType.Decimal).Value = dto.DiscountValue;
            cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = dto.Notes;
            cmd.Parameters.Add("@NumberOfCode", SqlDbType.Int).Value = dto.NumberOfCode;
            cmd.Parameters.Add("@Fix0_Per1", SqlDbType.Int).Value = dto.Fix0_Per1;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = dto.DiscountName;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = dto.DiscountCode;
            cmd.Parameters.Add("@FromDate", SqlDbType.Date).Value = dto.FromDate;
            cmd.Parameters.Add("@ToDate", SqlDbType.Date).Value = dto.ToDate;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Delete(int id)
        {
            SqlCommand cmd = new SqlCommand("update tblCM_Discounts set isdelete=1, DeleteDate=getdate() where Discountid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        internal bool CheckExistsName(string name, int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblCM_Discounts WHERE Discountid <> @id and Discountname = @name and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count > 0;
        }

        internal bool CheckBeforeDelete(int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT Discountid FROM tblCM_Orders o inner join tblCM_Discount d on Charindex(0.DiscountCode, d.discountcode)>0 where d.isdelete=0 and o.isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count == 0;
        }

        internal DataTable GetDiscountValue(decimal id, string code, string orderdate)
        {
            SqlCommand cmd = new SqlCommand("SP_CM_Discount_GetDiscountValueByCode", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.Decimal).Value = id;
            cmd.Parameters.Add("@code", SqlDbType.NVarChar).Value = code;
            cmd.Parameters.Add("@orderdate", SqlDbType.NVarChar).Value = orderdate;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
    }
}
